package sa.com.sure.ejtimaa.webservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.model.MeetingAttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingInfo;
import sa.com.sure.ejtimaa.model.MeetingItemInfo;
import sa.com.sure.ejtimaa.model.MeetingItemsAttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingPlaceInfo;
import sa.com.sure.ejtimaa.model.MeetingSessionInfo;
import sa.com.sure.ejtimaa.model.MeetingStatusInfo;
import sa.com.sure.ejtimaa.model.MemberInfo;
import sa.com.sure.ejtimaa.model.ReplyTypeInfo;

public class DataWebService {

	public static DataWebService Instance = new DataWebService();	
	
	public boolean Authenticate(String userName , String password) throws Exception{
		
		String params = String.format("<userName>%s</userName><password>%s</password>",userName,password);
		String res = "false";
		
		Document doc= Helper.GetHttpClientResult("Authenticate",params);	
		if(doc!=null){	
			Node child = doc.getElementsByTagName("AuthenticateResult").item(0).getFirstChild();
			
			res = child.getNodeValue();		
		}		
		
		return res.equalsIgnoreCase("true");
	}
		
	public List<MeetingPlaceInfo> GetMeetingPlaces() throws Exception{
		
		Document doc= Helper.GetHttpClientResult("GetMeetingPlaces","");
				
		List<MeetingPlaceInfo> items = new ArrayList<MeetingPlaceInfo>();
	
		if(doc!=null){
		
			NodeList infos = doc.getElementsByTagName("MeetingPlaceInfo");
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				int meetingPlaceID = Integer.parseInt(info.getChildNodes().item(0).getFirstChild().getNodeValue());
				
				String meetingPlaceName = info.getChildNodes().item(1).getFirstChild().getNodeValue();
				
				items.add(new MeetingPlaceInfo(meetingPlaceID,meetingPlaceName));
			}
		}
		return items;
	}
	
	public List<MeetingSessionInfo> GetMeetingSessions() throws Exception{
		
		Document doc= Helper.GetHttpClientResult("GetMeetingSessions","");
		
		List<MeetingSessionInfo> items = new ArrayList<MeetingSessionInfo>();
		
		if(doc!=null){	
		
			NodeList infos = doc.getElementsByTagName("MeetingSessionInfo");			
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				int SessionID = Integer.parseInt(info.getChildNodes().item(0).getFirstChild().getNodeValue());
				
				String SessionName = info.getChildNodes().item(1).getFirstChild().getNodeValue();
				
				items.add(new MeetingSessionInfo(SessionID,SessionName));
			}
		}
		
		return items;
	}
	
	public List<MeetingStatusInfo> GetMeetingStatuses() throws Exception{
		
		Document doc= Helper.GetHttpClientResult("GetMeetingStatuses","");
		List<MeetingStatusInfo> items = new ArrayList<MeetingStatusInfo>();
		
		if(doc!=null){	
		
			NodeList infos = doc.getElementsByTagName("MeetingStatusInfo");			
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				int MeetingStatusID = Integer.parseInt(info.getChildNodes().item(0).getFirstChild().getNodeValue());
				
				String MeetingStatusName = info.getChildNodes().item(1).getFirstChild().getNodeValue();
				
				items.add(new MeetingStatusInfo(MeetingStatusID,MeetingStatusName));
			}
		}
		return items;
	}
	
	public MemberInfo GetMember(String UserName) throws Exception{
		
		String params = String.format("<userName>%s</userName>",UserName.trim());
		
		Document doc= Helper.GetHttpClientResult("GetMember",params);
		
		MemberInfo item = null;
		
		if(doc!=null){	
		
			NodeList infos = doc.getElementsByTagName("GetMemberResult");			
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node getMemberResult = infos.item(i);
				
				Node baseProfile = getMemberResult.getChildNodes().item(1);
				
				Node profile = baseProfile.getChildNodes().item(1);
				
				item = new MemberInfo();										
				
				item.SetMemberID(getMemberResult.getFirstChild().getTextContent());
				item.SetUserName(profile.getChildNodes().item(1).getTextContent());	
				item.SetFullName(baseProfile.getChildNodes().item(2).getTextContent());
				item.SetEmail(profile.getChildNodes().item(2).getTextContent());
				item.SetMobile(baseProfile.getChildNodes().item(3).getTextContent());
				item.SetUserID(profile.getChildNodes().item(0).getTextContent());
				item.SetActive(profile.getChildNodes().item(3).getTextContent().equalsIgnoreCase("true"));
			}
		}
		return item;
	}
	
	
	public List<MemberInfo> GetApologists(int MeetingID) throws Exception{
		
		String params = String.format("<meetingID>%s</meetingID>",MeetingID);
		
		Document doc= Helper.GetHttpClientResult("GetApologists",params);
		
		MemberInfo item = null;
		List<MemberInfo> items = new ArrayList<MemberInfo>();
		
		if(doc!=null){	
		
			NodeList infos = doc.getElementsByTagName("MemberInfo");			
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node memberInfo = infos.item(i);
				
				Node baseProfile = memberInfo.getChildNodes().item(1);
										
				item = new MemberInfo();										
				
				item.SetMemberID(memberInfo.getFirstChild().getTextContent());
				item.SetFullName(baseProfile.getChildNodes().item(2).getTextContent());
				item.SetMobile(baseProfile.getChildNodes().item(3).getTextContent());
				item.SetActive(true);
				
				items.add(item);
			}
		}
		return items;
	}
	
	public List<MemberInfo> GetPresents(int MeetingID) throws Exception{
		
		String params = String.format("<meetingID>%s</meetingID>",MeetingID);
		
		Document doc= Helper.GetHttpClientResult("GetPresents",params);
		
		MemberInfo item = null;
		List<MemberInfo> items = new ArrayList<MemberInfo>();
		
		if(doc!=null){	
		
			NodeList infos = doc.getElementsByTagName("MemberInfo");			
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node memberInfo = infos.item(i);
				
				Node baseProfile = memberInfo.getChildNodes().item(1);						
				
				item = new MemberInfo();										
				
				item.SetMemberID(memberInfo.getFirstChild().getTextContent());
				item.SetFullName(baseProfile.getChildNodes().item(2).getTextContent());
				item.SetMobile(baseProfile.getChildNodes().item(3).getTextContent());
				item.SetActive(true);
				
				items.add(item);
			}
		}
		return items;
	}
	
	public List<ReplyTypeInfo> GetReplyTypes() throws Exception{
		
		Document doc= Helper.GetHttpClientResult("GetReplyTypes","");
		
		List<ReplyTypeInfo> items = new ArrayList<ReplyTypeInfo>();
		
		if(doc!=null){
		
			NodeList infos = doc.getElementsByTagName("ReplyTypeInfo");
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				int ReplyTypeID = Integer.parseInt(info.getChildNodes().item(0).getFirstChild().getNodeValue());
				
				String ReplyTypeName = info.getChildNodes().item(1).getFirstChild().getNodeValue();
				
				items.add(new ReplyTypeInfo(ReplyTypeID,ReplyTypeName));
			}
		
		}
		return items;
	}
		
	public List<MeetingInfo> GetMeetings() throws Exception{
		
		Document doc= Helper.GetHttpClientResult("GetMeetings","");
		
		List<MeetingInfo> items = new ArrayList<MeetingInfo>();
		
		if(doc!=null){
		
			NodeList infos = doc.getElementsByTagName("MeetingInfo");
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				
				int MeetingID = Integer.parseInt(info.getChildNodes().item(0).getFirstChild().getNodeValue());
				int SessionID =  Integer.parseInt(info.getChildNodes().item(1).getFirstChild().getFirstChild().getNodeValue());
				int MeetingStatusID = Integer.parseInt(info.getChildNodes().item(2).getFirstChild().getFirstChild().getNodeValue());
				int MeetingPlaceID =  Integer.parseInt(info.getChildNodes().item(3).getFirstChild().getFirstChild().getNodeValue());
				String Title = info.getChildNodes().item(4).getFirstChild().getNodeValue();
				Date StartDate  = Helper.GetDateFormat(info.getChildNodes().item(5).getFirstChild().getNodeValue(),Helper.DateFormatWebService);
				Date EndDate =  Helper.GetDateFormat(info.getChildNodes().item(7).getFirstChild().getNodeValue(),Helper.DateFormatWebService);
				String MeetingNo = info.getChildNodes().item(9).getFirstChild()==null?"":info.getChildNodes().item(9).getFirstChild().getNodeValue();
				String MeetingBrief = info.getChildNodes().item(10).getFirstChild() == null?"":info.getChildNodes().item(10).getFirstChild().getNodeValue();
				int WaitingForAttendeesReply =  Integer.parseInt(info.getChildNodes().item(11).getFirstChild().getNodeValue());
				Date MeetingStatusDateTime = Helper.GetDateFormat(info.getChildNodes().item(12).getFirstChild().getNodeValue(),Helper.DateFormatWebService);
				Date CreatedDateTime = Helper.GetDateFormat(info.getChildNodes().item(13).getFirstChild().getNodeValue(),Helper.DateFormatWebService);
				Date UpdatedDateTime = Helper.GetDateFormat(info.getChildNodes().item(14).getFirstChild().getNodeValue(),Helper.DateFormatWebService);
				
				
				items.add(new MeetingInfo(MeetingID,
											SessionID,
											MeetingStatusID,
											MeetingPlaceID,
											Title,
											StartDate,
											EndDate,
											MeetingNo,
											MeetingBrief,
											WaitingForAttendeesReply,
											MeetingStatusDateTime,
											CreatedDateTime,
											UpdatedDateTime));
			}
		
		}
		return items;
	}

	public List<MeetingItemInfo> GetMeetingItems(int MeetingID) throws Exception{
		
		String params = String.format("<meetingID>%s</meetingID>",Integer.toString(MeetingID));
		
		Document doc= Helper.GetHttpClientResult("GetMeetingItems",params);
		
		List<MeetingItemInfo> items = new ArrayList<MeetingItemInfo>();
		
		if(doc!=null){
		
			NodeList infos = doc.getElementsByTagName("MeetingItemInfo");
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				int MeetingItemID = Integer.parseInt(info.getChildNodes().item(0).getFirstChild().getNodeValue());
				String Title = info.getChildNodes().item(2).getFirstChild().getNodeValue();
				String Description = info.getChildNodes().item(3).getFirstChild()==null?"":info.getChildNodes().item(3).getFirstChild().getNodeValue();
				
				
				items.add(new MeetingItemInfo(MeetingItemID,MeetingID,Title,Description));
			}
		
		}
		return items;
	}
	
	public List<MeetingAttachmentInfo> GetMeetingAttachments(int MeetingID) throws Exception{
		
		String params = String.format("<meetingID>%s</meetingID>",Integer.toString(MeetingID));
		
		Document doc= Helper.GetHttpClientResult("GetMeetingAttachments",params);
		
		List<MeetingAttachmentInfo> items = new ArrayList<MeetingAttachmentInfo>();
		
		if(doc!=null){
		
			NodeList infos = doc.getElementsByTagName("AttachmentInfo");
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				String AttachmentID = info.getChildNodes().item(0).getFirstChild().getNodeValue();
				String FileName = info.getChildNodes().item(2).getFirstChild()==null?"":info.getChildNodes().item(2).getFirstChild().getNodeValue();
				int FileSize = Integer.parseInt(info.getChildNodes().item(3).getFirstChild().getNodeValue());				
				String FileType = info.getChildNodes().item(4).getFirstChild()==null?"":info.getChildNodes().item(4).getFirstChild().getNodeValue();				
				byte[] FileContent = Helper.ConvertfromBase64ToByteArray(info.getChildNodes().item(5).getFirstChild().getNodeValue());
				
				MeetingAttachmentInfo item = new MeetingAttachmentInfo();
				
				item.SetMeetingID(MeetingID);
				item.SetAttachmentID(AttachmentID);
				item.SetFileName(FileName);
				item.SetFileSize(FileSize);
				item.SetFileType(FileType);
				item.SetFileContent(FileContent);
				
				items.add(item);
			}
		
		}
		return items;
	}
	
	public List<MeetingItemsAttachmentInfo> GetMeetingItemAttachment(int MeetingItemID) throws Exception{
		
		String params = String.format("<meetingItemID>%s</meetingItemID>",Integer.toString(MeetingItemID));
		
		Document doc= Helper.GetHttpClientResult("GetMeetingItemAttachment",params);
		
		List<MeetingItemsAttachmentInfo> items = new ArrayList<MeetingItemsAttachmentInfo>();
		
		if(doc!=null){
		
			NodeList infos = doc.getElementsByTagName("AttachmentInfo");
			
			for(int i=0; i<infos.getLength(); i++) {
				
				Node info = infos.item(i);
							
				String AttachmentID = info.getChildNodes().item(0).getFirstChild().getNodeValue();
				String FileName = info.getChildNodes().item(2).getFirstChild().getNodeValue();
				int FileSize = Integer.parseInt(info.getChildNodes().item(3).getFirstChild().getNodeValue());				
				String FileType = info.getChildNodes().item(4).getFirstChild()==null?"":info.getChildNodes().item(4).getFirstChild().getNodeValue();				
				byte[] FileContent = Helper.ConvertfromBase64ToByteArray(info.getChildNodes().item(5).getFirstChild().getNodeValue());
				
				MeetingItemsAttachmentInfo item = new MeetingItemsAttachmentInfo();
				
				item.SetMeetingItemID(MeetingItemID);
				item.SetAttachmentID(AttachmentID);
				item.SetFileName(FileName);
				item.SetFileSize(FileSize);
				item.SetFileType(FileType);
				item.SetFileContent(FileContent);
				
				items.add(item);
			}
		
		}
		return items;
	}
}

